package com.lehecai.core.task.entity.status;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 异步任务状态
 * Created by qatang on 14-3-26.
 */
public enum AsyncTaskStatus {
    ALL("全部"),
    CREATED("已创建"),
    SENT("已发送至队列"),
    RECEIVED("已从队列接收"),
    EXECUTING("开始执行"),
    DONE("已完成"),
    OBSOLETED("已作废");

    private static Logger logger = LoggerFactory.getLogger(AsyncTaskStatus.class);

    private static final Map<Integer, AsyncTaskStatus> _MAP = new HashMap<Integer, AsyncTaskStatus>();
    private static List<AsyncTaskStatus> _LIST = new ArrayList<AsyncTaskStatus>();
    private static List<AsyncTaskStatus> _ALL_LIST = new ArrayList<AsyncTaskStatus>();
    static {
        for(AsyncTaskStatus asyncTaskType : AsyncTaskStatus.values()){
            _MAP.put(asyncTaskType.getValue(), asyncTaskType);
            _ALL_LIST.add(asyncTaskType);
            if (!asyncTaskType.equals(ALL)) {
                _LIST.add(asyncTaskType);
            }
        }

        synchronized (_LIST) {
            _LIST = Collections.unmodifiableList(_LIST);
        }
        synchronized (_LIST) {
            _ALL_LIST = Collections.unmodifiableList(_ALL_LIST);
        }
    }

    private String name;

    private AsyncTaskStatus(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public int getValue(){
        return this.ordinal();
    }

    public static AsyncTaskStatus get(int value){
        try{
            return _MAP.get(value);
        }catch(Exception e){
            logger.error(e.getMessage(), e);
            return null;
        }
    }

    public static List<AsyncTaskStatus> list() {
        return _LIST;
    }

    public static List<AsyncTaskStatus> listAll() {
        return _ALL_LIST;
    }
}
